package com.ksyun.campus.dataserver.bootstrap;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author zhaozhikai
 * @date 2023/8/8 2:22
 */
@Component
@Slf4j
public class Root {
    /**
     * 初始化 本地存储路径
     */
    @Async
    public void initPath() {
        String dataPath = DataServerInstance.getDataPath();
        File file = new File(dataPath);
        if (!file.exists()) {
            boolean mkdir = file.mkdirs();
            if (!mkdir) {
                throw new RuntimeException(String.format("初始化存储路径失败,请检查路径是否合法:%s", dataPath));
            }
            log.info("构建路径：{}", dataPath);
        }else{
            log.info("扫描磁盘文件~");
            scanFile(file);
        }
    }

    /**
     *  扫描本地文件
     * @param file
     */
    private void scanFile(File file){
        File[] files = file.listFiles();
        int fileTotal = 0;
        int UseCapacity = 0;
        for(int i = 0; i < files.length; i ++){
            if(files[i].isFile()){
                UseCapacity += files[i].length();
                fileTotal++;
            }
        }
        //更新节点服务信息
        DataServerInstance.updateInfo(fileTotal,UseCapacity);
    }

}
